package com.tothefor.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tothefor.pojo.dto.GroupInfoDTO;
import com.tothefor.pojo.entity.Group;
import com.tothefor.pojo.entity.User;
import com.tothefor.resultR.R;
import com.tothefor.service.impl.GroupServiceImpl;
import com.tothefor.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * sun
 */
@RestController
@RequestMapping("/group")
public class groupController {
    @Autowired
    private GroupServiceImpl groupService;

    @Autowired
    private UserServiceImpl userService;




    @PostMapping("/usergroupAll")
    public R groupAll(){
        List<Group> groups=groupService.list();
        List<User> users=userService.list();

        Page<Group> userPage = new Page<>(1, 2);
        groupService.page(userPage,null);

        List<Group> result=userPage.getRecords();

        System.out.println(result);

        List<Group> groups1=new ArrayList<>();

        //处理groups，选择用户群组
        for(Group group:groups){
            if(group.getType().equals("answer")){
                groups1.add(group);
            }
        }

        groups.removeAll(groups1);

        //根据id获取所有组员
        int i=0;
        for(Group group:groups){
            List<User> users1=new ArrayList<>();
            for(User user:users){
                if(user.getGroupid()==group.getId()){
                    users1.add(user);
                }
            }
            group.setUsers(users1);
            groups.set(i,group);
            i++;
        }


        if(groups!=null){
            GroupInfoDTO groupInfoDTO=new GroupInfoDTO();
            groupInfoDTO.setGroups(groups);
            return R.SUCCESS(groupInfoDTO);
        }else {
            return R.FAIL();
        }

    }

    @PostMapping("/answergroupAll")
    public R answergroupAll(){
        List<Group> groups=groupService.list();
        List<User> users=userService.list();

        List<Group> groups1=new ArrayList<>();

        //处理groups，选择用户群组
        for(Group group:groups){
            if(group.getType().equals("user")){
                groups1.add(group);
            }
        }

        groups.removeAll(groups1);

        //根据id获取所有组员
        int i=0;
        for(Group group:groups){
            List<User> users1=new ArrayList<>();
            for(User user:users){
                if(user.getGroupid()==group.getId()){
                    users1.add(user);
                }
            }
            group.setUsers(users1);
            groups.set(i,group);
            i++;
        }


        if(groups!=null){
            GroupInfoDTO groupInfoDTO=new GroupInfoDTO();
            groupInfoDTO.setGroups(groups);
            return R.SUCCESS(groupInfoDTO);
        }else {
            return R.FAIL();
        }

    }

    @PostMapping("/deleteusergroup")
    public R deleteusergroup(@RequestBody int id){
        //判断该id对应的群组下，是否有用户
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("groupid",id);
        List<User> users=userService.list(queryWrapper);
        if(users.size()==0){
            //处理删除
            QueryWrapper<Group> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("id",id);
            if(groupService.remove(queryWrapper1)){
                System.out.println("删除成功");
                return groupAll();
            }else {
                R.FAIL();
            }

        }else{
            return R.FAIL();
        }

        return R.FAIL();
    }

    @PostMapping("/addUserGru")
    public R addUserGru(@RequestBody  GroupInfoDTO groupInfoDTO){
        //检查群组名称是否重复
        QueryWrapper<Group> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",groupInfoDTO.getName());
        List<Group> groups=groupService.list(queryWrapper);
        if(groups.size()==0){
            //创建新的用户群组
            Group group=new Group();
            group.setName(groupInfoDTO.getName());
            group.setType("user");
            group.setState("正常");
            group.setDescription(groupInfoDTO.getDescription());
            if(groupService.save(group)){
                //处理用户，为用户的Group_id赋值
                QueryWrapper<Group> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("name",groupInfoDTO.getName());
                groups=groupService.list(queryWrapper);
                group=groups.get(0);
                List<User> users=groupInfoDTO.getUsers();
                for(User user:users){
                    //设置group_id
                    user.setGroupid(group.getId());
                    userService.updateById(user);
                }
                return  R.SUCCESS();
            }else {
                return R.FAIL();
            }
        }else {
            return R.FAIL();
        }

    }



    @PostMapping("/addAnswerGru")
    public R addAnswerGru(@RequestBody  GroupInfoDTO groupInfoDTO){
        //检查群组名称是否重复
        QueryWrapper<Group> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",groupInfoDTO.getName());
        List<Group> groups=groupService.list(queryWrapper);
        if(groups.size()==0){
            //创建新的用户群组
            Group group=new Group();
            group.setName(groupInfoDTO.getName());
            group.setType("answer");
            group.setState("正常");
            group.setDescription(groupInfoDTO.getDescription());
            if(groupService.save(group)){
                //处理用户，为用户的Group_id赋值
                QueryWrapper<Group> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("name",groupInfoDTO.getName());
                groups=groupService.list(queryWrapper);
                group=groups.get(0);
                List<User> users=groupInfoDTO.getUsers();
                for(User user:users){
                    //设置group_id
                    user.setGroupid(group.getId());
                    userService.updateById(user);
                }
                return  R.SUCCESS();
            }else {
                return R.FAIL();
            }
        }else {
            return R.FAIL();
        }

    }
    /*
    * sun
    * 作用：获取id对应的群组
    * */
    @PostMapping("/userGru")
    public R userGru(@RequestBody  GroupInfoDTO groupInfoDTO){
        int id=groupInfoDTO.getId();
        QueryWrapper<Group> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Group group=groupService.getOne(queryWrapper);
        if(group!=null){
            groupInfoDTO.setName(group.getName());
            groupInfoDTO.setDescription(group.getDescription());
            return R.SUCCESS(groupInfoDTO);
        }else {
            return R.FAIL();
        }
    }

    /**
     * sun
     *
     *将前端对群组的更新同步到后端
     */
    @PostMapping("/updateUserGru")
    public R updateUserGru(@RequestBody  GroupInfoDTO groupInfoDTO){
        int id=groupInfoDTO.getId();
        QueryWrapper<Group> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Group group=groupService.getOne(queryWrapper);
        if(group!=null){
            //对group进行修改
            group.setName(groupInfoDTO.getName());
            group.setDescription(groupInfoDTO.getDescription());
            //数据库设计：用户单方面维护与群组的关系
            List<User> users=groupInfoDTO.getUsers();
            for(User user:users){
                //设置group_id
                user.setGroupid(group.getId());
                userService.updateById(user);
            }
            if(groupService.updateById(group)){
                return R.SUCCESS();
            }else {
                return R.FAIL();
            }
        }else {
            return R.FAIL();
        }
    }

    /**
     * sun
     *
     * 删除答者群组
     * @param id
     * @return
     */

    @PostMapping("/deleteanswergroup")
    public R deleteanswergroup(@RequestBody int id){
        //判断该id对应的群组下，是否有用户
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("groupid",id);
        List<User> users=userService.list(queryWrapper);
        if(users.size()==0){
            //处理删除
            QueryWrapper<Group> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("id",id);
            if(groupService.remove(queryWrapper1)){
                return answergroupAll();
            }else {
                R.FAIL();
            }

        }else{
            return R.FAIL();
        }

        return R.FAIL();
    }





}
